#测试聊天室登陆页面
import time
from lib2to3.pgen2 import driver

from selenium.webdriver.common.by import By

from common.Utils import Driver, ChatDriver


class ChatLogin:
    url = ""
    driver = ""
    ChatDriver = ""
    def __init__(self):
        self.url = "http://127.0.0.1:8080/login.html"
        self.ChatDriver = Driver()
        self.driver = self.ChatDriver.driver
        self.driver.get(self.url)
        # 成功登陆的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 切换到警告框
        time.sleep(3)
        alert = self.driver.switch_to.alert
        text = alert.text
        #print(f"警告框中的文本: {text}")  # 打印获取到的文本
        alert.accept()
        assert text == "登录成功!"
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        #返回登录页面
        self.driver.back()

        #未成功登录的测试用例
    def LoginFailTest(self):
        #异常用户名
        #账号错误，密码正确
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan..")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 切换到警告框
        time.sleep(2)
        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "登录失败!"
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        alert.accept()
        #账号密码为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)
        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前输入的用户名或者密码为空!"
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        alert.accept()

        #账号为空，密码正确
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)

        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前输入的用户名或者密码为空!"
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        alert.accept()

        #异常密码
        # 账号正确，密码错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 切换到警告框
        time.sleep(2)

        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "登录失败!"
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        alert.accept()

        # 账号密码为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)

        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前输入的用户名或者密码为空!"
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        alert.accept()

        # 账号正确，密码为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)

        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前输入的用户名或者密码为空!"
        # 添加屏幕截图
        ChatDriver.get_alert_image()
        alert.accept()

        #不点击登录按钮
        #正确的账号密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        #self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav")

        #错误的账号正确的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan123")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav")

        #正确的账号错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav")
        # 添加屏幕截图
        ChatDriver.get_alert_image()

Login = ChatLogin()
Login.LoginSucTest()
#Login.LoginFailTest()